package com.ycode.bang.anbang.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.ycode.bang.anbang.MyApp;
import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.baseui.BaseActivity;
import com.ycode.bang.anbang.baseui.BaseFragment;
import com.ycode.bang.anbang.fragment.PubDoingFragment;
import com.ycode.bang.anbang.fragment.PubDoneFragment;
import com.ycode.bang.anbang.fragment.PubNewFragment;
import com.ycode.bang.anbang.fragment.PubPlayingFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;
import butterknife.InjectView;

/**
 * Created by maosongsong on 2016/4/6.
 */

public class AcceptPublicOrderActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.title)
    TextView title;
    @InjectView(R.id.btn_handle_new)
    Button mBtnNew;
    @InjectView(R.id.btn_handle_doing)
    Button mBtnDoing;
    @InjectView(R.id.btn_handle_playing)
    Button mBtnPlaying;
    @InjectView(R.id.btn_handle_done)
    Button mBtnDone;
    public BaseFragment mNewFragment;
    private BaseFragment mDoingFragment;
    private BaseFragment mPlayingFragment;
    private BaseFragment mDoneFragment;
    private List<BaseFragment> mFragmentList;
    private int mPosition;
    private receivePushMassage receivePushMassage = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addOurView(R.layout.activity_accept_public_order);
        ButterKnife.inject(this);
        initData(mBtnNew);
        initView();
//        getBundle();
        registMassage();
    }

//    private void getBundle() {
//        String type = getIntent().getStringExtra("FjpushType");
//        if (!TextUtils.isEmpty(type)) {
//            switch (type) {
//                case "ORDER_DELIVERED":
//                    mBtnDelivery.performClick();
//                    break;
//                case "ORDER_WAIT_QUOTE":
//                    mBtnWait.performClick();
//                    break;
//            }
//        }
//    }


    private void initData(Button button) {
        button.setSelected(true);
        mFragmentList = new ArrayList<>();
        mNewFragment = new PubNewFragment();
        mDoingFragment = new PubDoingFragment();
        mPlayingFragment = new PubPlayingFragment();
        mDoneFragment = new PubDoneFragment();
        mFragmentList.add(mNewFragment);
        mFragmentList.add(mDoingFragment);
        mFragmentList.add(mPlayingFragment);
        mFragmentList.add(mDoneFragment);

    }

    private void initView() {
        if(MyApp.instance.isManage()){
            mBtnNew.setText("待接收");
        }else {
            mBtnPlaying.setVisibility(View.GONE);
        }
        title.setText("公共报修");
        mBtnNew.setOnClickListener(this);
        mBtnDoing.setOnClickListener(this);
        mBtnPlaying.setOnClickListener(this);
        mBtnDone.setOnClickListener(this);
                getSupportFragmentManager().beginTransaction().add(R.id.fl_order, mNewFragment)
                .add(R.id.fl_order, mDoingFragment).add(R.id.fl_order, mPlayingFragment).add(R.id.fl_order, mDoneFragment)
                .hide(mDoingFragment).hide(mPlayingFragment).hide(mDoneFragment)
                .show(mNewFragment).commit();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_handle_new:
                if (!mBtnNew.isSelected()) {
                    mBtnNew.setSelected(true);
                    mBtnDoing.setSelected(false);
                    mBtnPlaying.setSelected(false);
                    mBtnDone.setSelected(false);
                    switchFragment(0);
                }
                break;
            case R.id.btn_handle_doing:
                if (!mBtnDoing.isSelected()) {
                    mBtnDoing.setSelected(true);
                    mBtnNew.setSelected(false);
                    mBtnPlaying.setSelected(false);
                    mBtnDone.setSelected(false);
                    switchFragment(1);
                }
                break;
            case R.id.btn_handle_playing:
                if (!mBtnPlaying.isSelected()) {
                    mBtnPlaying.setSelected(true);
                    mBtnNew.setSelected(false);
                    mBtnDoing.setSelected(false);
                    mBtnDone.setSelected(false);
                    switchFragment(2);
                }
                break;
            case R.id.btn_handle_done:
                if (!mBtnDone.isSelected()) {
                    mBtnDone.setSelected(true);
                    mBtnNew.setSelected(false);
                    mBtnDoing.setSelected(false);
                    mBtnPlaying.setSelected(false);
                    switchFragment(3);
                }
                break;

        }
    }

    /**
     * 切换不同的fragment
     *
     * @param postion 切换的位置
     */
    public void switchFragment(int postion) {
        mPosition = postion;
        int cur1 = (postion + 1) % 4;
        int cur2 = (postion + 2) % 4;
        int cur3 = (postion + 3) % 4;
        getSupportFragmentManager().beginTransaction().show(mFragmentList.get(postion))
                .hide(mFragmentList.get(cur1)).hide(mFragmentList.get(cur2)).hide(mFragmentList.get(cur3))
                .commit();
        mFragmentList.get(postion).onResume();
    }


    private void registMassage() {
        //注册广播接收到消息便自动刷新界面
        IntentFilter filter1 = new IntentFilter("action_jpush_check");
        if (receivePushMassage == null) {
            receivePushMassage = new receivePushMassage();
            registerReceiver(receivePushMassage, filter1);
        }
    }

    class receivePushMassage extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle extras = intent.getExtras();
            if (extras == null)
                return;
            String title = extras.getString("title");
            String json = extras.getString("type");
            mFragmentList.get(mPosition).onResume();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(receivePushMassage);
    }

    @Override
    protected void getSuccessRequest(Object response, int flag) {

    }
}
